// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

   // 登录
   wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      wx.request({
        url: this.globalData.url + 'http://localhost:8099/wxapi/login/wxLogin', //请求的接口地址
        data: {
          code: res.code
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          if (res.data.code == 200) {
            if (res.data.data.openid) {
              //存储openid到全局变量，当然也可以同时存储到作用域中
              this.globalData.openid = res.data.data.openid
              this.globalData.session_key = res.data.data.session_key
              console.log(this.globalData.openid)
              console.log(this.globalData.session_key)
              wx.setStorageSync('openid', res.data.data.openid)
            }
          }
        }
      })
    }
  })
},
//全局变量
globalData: {
  userInfo: null,
  url: 'http://localhost:8099', //请求的url
  openid: '',
  session_key: '',
  token: ''
  }
})
